package com.plugin.manage.controller;

import com.plugin.core.util.GraphicRandNumUtil;
import com.plugin.core.util.MyBase64Util;
import com.plugin.core.bean.LogAop;
import com.plugin.manage.common.RespObj;
import com.plugin.manage.pojo.dto.TestDTO;
import com.plugin.manage.service.ISysAuthService;
import com.plugin.manage.test.AppDeepseek;
import com.plugin.manage.util.HttpClientUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/mgr")
@Tag(name = "登录接口", description = "系统登录相关接口")
@Slf4j
public class SysAuthController {

    @Autowired
    private ISysAuthService sysAuthService;

    @Autowired
    private AppDeepseek appDeepseek;

    /**
     * 用户登录
     * 
     * @return 登录结果
     */
    @PostMapping("/login")
    @Operation(summary = "用户登录", description = "用户登录接口，返回Token")
    public RespObj<?> authLogin(@RequestParam(value = "username", required = true) String username,
                                @RequestParam(value = "password", required = true) String password) {
        return sysAuthService.login(username, password);
    }

    /**
     * 用户注册
     * 
     * @param registerRequest 注册请求参数
     * @return 注册结果
     */
    @PostMapping("/register")
    @Operation(summary = "用户注册", description = "用户注册接口")
    public RespObj<?> register(@RequestBody Map<String, String> registerRequest) {
        String username = registerRequest.get("username");
        String password = registerRequest.get("password");
        String email = registerRequest.get("email");
        String phone = registerRequest.get("phone");
        return sysAuthService.register(username, password, email, phone);
    }

    /**
     * java生成图片并用io流传给前端一个图片的base64字符串
     * @return
     */
    @PostMapping("/captcha")
    @Operation(summary = "验证码", description = "验证码接口")
    @LogAop(remark = "验证码校验")
    public RespObj<?> getVerifCode() {

        Map<String,Object> obj = new HashMap<>();
        Map<String, Object> randomMap = GraphicRandNumUtil.getRandcode(120, 40, 6, 20);
        String randomCode = randomMap.get("vercode").toString();
        BufferedImage image = (BufferedImage) randomMap.get("randomImage");
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

        try {
            ImageIO.write(image, "jpg", outputStream);
            String base64 = MyBase64Util.encode2String(outputStream.toByteArray()).trim();
            base64 = base64.replaceAll("\n", "").replaceAll("\r", "");
            obj.put("code", "data:image/jpg;base64," + base64);
            obj.put("randomCode", randomCode);

        } catch (IOException e) {
            log.error("exception :", e);
        } finally {
            try {
                outputStream.flush();
                outputStream.close();
            } catch (IOException e) {
                log.error("---------exception:{}", e);
            }
        }

        return new RespObj<>().success(obj);
    }


    @PostMapping("/test")
    @Operation(summary = "测试接口", description = "测试接口详细描述")
    public RespObj<?> getLogin(@RequestBody TestDTO testDTO) {


        //String s = appDeepseek.chatWithDeepseek(message);


        log.info("AAAAAA");
        return new RespObj<>().success(null);
    }
}
